<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="fullName">
              <el-input v-model="filters.fullName" placeholder="真实姓名" clearable></el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="filters.phone" placeholder="手机号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="idCard">
              <el-input v-model="filters.idCard" placeholder="身份证号" clearable></el-input>
            </el-form-item>
            <el-form-item prop="userType">
              <el-select v-model="filters.userType" placeholder="用户类型" clearable>
                <el-option v-for="item in userTypeList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="checkState">
              <el-select v-model="filters.checkState" placeholder="审核类型" clearable>
                <el-option v-for="item in checkStateList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table ref="table" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column label="用户ID" prop="userId" width="150" align="center"> </el-table-column>
        <el-table-column label="用户修改信息类型" width="150" align="center">
          <template slot-scope="scope">
            <div>
              {{ formatUpdateFlag(scope.row.updateFlag) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户类型" width="100" align="center">
          <template slot-scope="scope">
            <div>
              {{ formatUserType(scope.row.userType) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="真实姓名" prop="fullName" width="140" align="center"> </el-table-column>
        <el-table-column label="手机号" prop="phone" width="160" align="center"> </el-table-column>
        <el-table-column label="身份证" prop="idCard" width="200" align="center"> </el-table-column>
        <el-table-column label="审核状态" width="110" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" effect="dark" :type="formatCheckStates(scope.row.checkState)">
              {{ formatCheckState(scope.row.checkState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="checkUserName" align="center" width="140" label="审核人"></el-table-column>
        <el-table-column prop="checkDate" align="center" width="240" label="审核时间"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionSee" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <!-- <el-button v-has="permissionAccountCheck" @click="handleClickCheck(scope.row)" size="mini">审核</el-button> -->
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionAccountCheck" v-if="scope.row.checkState === 0" @click.native="handleClickCheck(scope.row, 1)">审核通过</el-dropdown-item>
                <el-dropdown-item v-has="permissionAccountCheck" v-if="scope.row.checkState === 0" @click.native="handleClickCheck(scope.row, 2)">审核失败</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
    <el-dialog :visible.sync="check.show" title="审核失败原因" center width="fit-content">
      <el-input v-model="check.checkReason" rows="6" style="width: 300px" type="textarea"></el-input>
      <div slot="footer">
        <el-button @click="confirmFailCheck" type="primary">确定</el-button>
        <el-button @click="check.show = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import YmAccountCheckApi from '@/api/account/ymAccountCheck';

export default {
  name: 'AccountCheckManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        fullName: '',
        idCard: '',
        phone: '',
        userType: '',
        checkState: '', //0待审核 1审核成功 2审核失败
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      userTypeList: [
        { id: 1, name: '企业商户' },
        { id: 2, name: '个体工商户' },
        { id: 3, name: '个人' },
      ],
      checkStateList: [
        { id: 0, name: '待审核' },
        { id: 1, name: '审核成功' },
        { id: 2, name: '审核失败' },
      ],
      // 权限

      permissionSee: 'SYSTEM_MANAGER_ACCOUNT_YMACCOUNTCHECK_LIST_SEE',
      permissionAccountCheck: 'SYSTEM_MANAGER_ACCOUNT_YMACCOUNTCHECK_LIST_ACCOUNT_CHECK',
      check: {
        show: false,
        checkReason: '',
      },
      rowItem: '',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      const { checkState, fullName, idCard, phone, userType } = this.filters;
      var cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        checkState,
        fullName,
        idCard,
        phone,
        userType,
      };
      // 发送请求
      YmAccountCheckApi.listAccountCheck(cond)
        .then((res) => {
          this.listLoading = false;
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$refs.table.bodyWrapper.scrollTop = 0;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    formatUserType(cellValue) {
      if (cellValue === 1) {
        return '企业商户';
      } else if (cellValue === 2) {
        return '个体工商户';
      } else if (cellValue === 3) {
        return '个人';
      }
    },
    formatUpdateFlag(cellValue) {
      if (cellValue === 1) {
        return '新增账户信息';
      } else if (cellValue === 2) {
        return '修改账户信息';
      } else if (cellValue === 3) {
        return '更换结算卡';
      }
    },
    formatCheckState(cellValue) {
      if (cellValue === 0) {
        return '待审核';
      } else if (cellValue === 1) {
        return '审核成功';
      } else if (cellValue === 2) {
        return '审核失败';
      }
    },
    formatCheckStates(cellValue) {
      if (cellValue === 0) {
        return '';
      } else if (cellValue === 1) {
        return 'success';
      } else if (cellValue === 2) {
        return 'danger';
      }
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/account/ymAccountCheckDetail',
        query: { id: row.accountCheckId },
      });
    },
    handleClickCheck(row, state) {
      this.rowItem = row;
      if (state == 1) {
        this.$confirm('确定允许通过审核？', '提示', {}).then(() => {
          const params = {
            accountCheckId: row.accountCheckId,
            checkState: 1,
          };
          YmAccountCheckApi.accountCheck(params).then(() => {
            this.$Utils.notifySuccess('操作成功');
            this.listData();
          });
        });
      } else {
        this.check.show = true;
      }
    },
    confirmFailCheck() {
      const { rowItem, check } = this;
      if (!check.checkReason) {
        this.$message.error('请输入审核失败的原因');
        return;
      }
      this.$confirm('确定审核失败', '提示', {}).then(() => {
        const params = {
          accountCheckId: rowItem.accountCheckId,
          checkState: 2,
          checkReason: check.checkReason,
        };
        YmAccountCheckApi.accountCheck(params).then(() => {
          this.$Utils.notifySuccess('操作成功');
          this.listData();
        });
        check.show = false;
      });
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped></style>
